﻿using CoreShop.Library;
using CoreShop.Repository;
using CoreShop.Services;
using SqlSugar.Extensions;
using Sunny.UI;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Windows.Forms;

namespace CoreShop.App
{
    public partial class frmSystemUpdate : UIForm
    {
        public frmSystemUpdate()
        {
            InitializeComponent();
        }

        private void frmDBConnect_Load(object sender, EventArgs e)
        {
            try
            {
                MyConstVars.updateUrl = IniFiles.ReadIniData("FilesUpdate", "https").ObjToString();
                MyConstVars.softName = IniFiles.ReadIniData("FilesUpdate", "name").ObjToString();
            }
            catch { }
        }

        private void Downfile_setBarprogress(long size)
        {
            this.uiProcessBar1.Invoke(new Action(() =>
            {
                if (size <= this.uiProcessBar1.Maximum)
                {
                    this.uiProcessBar1.Value = (int)size;
                    decimal cur = (decimal)this.uiProcessBar1.Value;
                    decimal max = (decimal)this.uiProcessBar1.Maximum;
                    this.lbl_msg.Text = "当前下载进度:" + (cur / max).ToString("p");
                }
                else
                {
                    this.lbl_msg.Text = "当前下载进度:100%";
                    this.uiProcessBar1.Value = this.uiProcessBar1.Maximum;
                }
            }));
        }

        private void Downfile_initalProgressbar(long size)
        {
            this.uiProcessBar1.Invoke(new Action(() =>
            {
                this.uiProcessBar1.Maximum = (int)size + 1024;
                this.uiProcessBar1.Value = 0;
            }));
        }

        public void DataExc_SetLog(string msg)
        {
            if (msg == "ok")
            {
                string toolPath = Application.StartupPath + "\\UpgradeWork.exe";
                if (File.Exists(toolPath))
                {
                    Process.Start(toolPath);
                    Application.Exit();
                }
            } else
            {
                this.rtxDataLog.Invoke(new Action(() => {
                    this.rtxDataLog.AppendText(msg);
                    this.rtxDataLog.HideSelection = false;
                }));
            }
        }

        private void btnDownUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                if (MyConstVars.DownFileFlag == false)
                {
                    MyConstVars.DownFileFlag = true;
                    DownFile downfile = new DownFile();
                    downfile.CheckVersion = false;
                    downfile.setBarprogress += Downfile_setBarprogress;
                    downfile.initalProgressbar += Downfile_initalProgressbar;
                    downfile.SetLog += DataExc_SetLog;
                    Thread Syn_thread = new Thread(downfile.DownLoad);
                    Syn_thread.Start();
                }
            }
            catch { }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
